#import "ALEditTemplateViewController.h"
#import "ALModelHeaders.h"
#import <SMRColumnView/SMRColumnView.h>
#import <SMRColumnView/SMRColumnEntry.h>


@interface ALEditTemplateViewController ()
@property (nonatomic, retain) ALEntry *selectedEntry;
@property (nonatomic, retain) NSArray *elementsSortDescriptors;
@end


#pragma mark -
@implementation ALEditTemplateViewController

@synthesize elementsColumnView;
@synthesize messageTextField;
@synthesize selectedEntry = selectedEntry_;
@synthesize elementsSortDescriptors;


#pragma mark -
#pragma mark Initialization

-(void)awakeFromNib {
	[self setElementsSortDescriptors:[NSArray arrayWithObject:[[NSSortDescriptor alloc] initWithKey:ALLineNumberKey ascending:YES]]];
	NSNotificationCenter *noteCenter = [NSNotificationCenter defaultCenter];
	[noteCenter addObserver:self selector:@selector(entrySelectionDidChange:) name:SMRColumnViewSelectionDidChangeNotification object:[self valueForKeyPath:@"parent.eventViewController.eventColumnView"]];
}


#pragma mark -
#pragma mark Notifications

-(void)entrySelectionDidChange:(NSNotification *)note {
	SMRColumnView *entriesColumnView = [note object];
	NSArray *selectedObjects = [entriesColumnView selectedObjects];
	NSInteger selectionCount = [selectedObjects count];
	[[self messageTextField] setHidden:(selectionCount == 1)];
	[[self elementsColumnView] setHidden:(selectionCount != 1)];
	if (selectionCount != 1) {
		[[self messageTextField] setStringValue:(selectionCount == 0) ? ALTemplateEditorNothingSelected : ALTemplateEditorMultipleSelection];
		[self setSelectedEntry:nil];
		return;
	}
	NSAssert([[selectedObjects objectAtIndex:0] isKindOfClass:[ALEntry class]], @"ALEditTemplateViewController entrySelectionDidChange: selection object is not of class ALEntry");
	ALEntry *entry = [selectedObjects objectAtIndex:0];
	[self setSelectedEntry:entry];
	[[self elementsColumnView] reloadData];
}


#pragma mark -
#pragma mark Actions

-(IBAction)addElementTemplate:(id)sender {
	SMRColumnEntry *clickedEntry = [[self elementsColumnView] entryWithView:sender];
	ALElementTemplate *clickedElementTemplate = [clickedEntry representedObject];
	ALEntryTemplate *selectedTemplate = [[self selectedEntry] template];
	ALElementTemplate *addedElement = [ALElementTemplate insertInManagedObjectContext:[self managedObjectContext]];
	[selectedTemplate insertElementTemplate:addedElement atLineNumber:[[clickedElementTemplate lineNumber] integerValue] + 1];
	[[self elementsColumnView] reloadData];
	
}

#pragma mark -
#pragma mark Column view data source methods

-(NSUInteger)columnView:(SMRColumnView *)aColumnView numberOfChildEntriesForParentEntry:(SMRColumnEntry *)aColumnEntry {
	if (aColumnEntry != nil || ![self selectedEntry])
		return 0;
	NSSet *elements = [[self selectedEntry] elements];
	return [elements count];
}

-(id)columnView:(SMRColumnView *)aColumnView parentEntry:(SMRColumnEntry *)aColumnEntry objectValueForEntryAtIndex:(NSUInteger)entryIndex {
	if (aColumnEntry != nil || ![self selectedEntry])
		return nil;
	NSSet *elements = [[[self selectedEntry] template] elementTemplates];
	NSArray *sortedElements = [elements sortedArrayUsingDescriptors:[self elementsSortDescriptors]];
	return [sortedElements objectAtIndex:entryIndex];
}


#pragma mark -
#pragma mark Column view delegate methods

-(NSView *)columnView:(SMRColumnView *)aColumnView willForwardHitTestAtPoint:(NSPoint)aPoint toSubview:(NSView *)subview {
	if ([subview isKindOfClass:[NSButton class]])
		return subview;
	return aColumnView;
}

@end
